package com.ruoyi.system.utils;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

public class DirectoryUtil {


    //删除文件夹
    public static void deleteDirectory(Path directory) throws IOException {
        // 首先检查目录是否存在
        if (Files.exists(directory)) {
            // 遍历目录中的所有文件和子目录
            Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    // 删除文件
                    Files.delete(file);
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    // 删除目录
                    Files.delete(dir);
                    return FileVisitResult.CONTINUE;
                }
            });
        } else {
            System.out.println("Directory not found: " + directory.toString());
        }
    }


    //复制文件
    public static void copyFolder(Path sourceDir, Path targetDir) throws IOException {
        // 如果目标文件夹不存在，则创建
        if (!Files.exists(targetDir)) {
            Files.createDirectories(targetDir);
        }

        // 遍历源文件夹中的所有文件和子文件夹
        Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                // 构建目标文件夹中对应的子文件夹路径
                Path targetDirPath = targetDir.resolve(sourceDir.relativize(dir));
                // 确保目标文件夹存在
                if (!Files.exists(targetDirPath)) {
                    Files.createDirectories(targetDirPath);
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                // 构建目标文件路径
                Path targetFilePath = targetDir.resolve(sourceDir.relativize(file));
                // 复制文件
                Files.copy(file, targetFilePath, StandardCopyOption.REPLACE_EXISTING);
                return FileVisitResult.CONTINUE;
            }
        });
    }


    //递归文件目录
    public static List<String> traverseDirectories(String rootPath) {
        List<String> matchingDirectories = new ArrayList<>();

        File rootDir = new File(rootPath);
        File[] subDirs = rootDir.listFiles(File::isDirectory);

        if (subDirs != null&&subDirs.length==1) {
            for (File subDir : subDirs) {
                // Check if subDir has exactly one subdirectory
                File[] subSubDirs = subDir.listFiles(File::isDirectory);

                if (subSubDirs != null) {

                    for(int i=0;i<subSubDirs.length;i++){
                        File[] niiFiles = subSubDirs[i].listFiles((dir, name) -> name.toLowerCase().endsWith(".nii"));

                        if (niiFiles != null && niiFiles.length == 1) {
                            matchingDirectories.add(subSubDirs[i].getName());
                        }
                    }
                }
            }
        }
        return matchingDirectories;
    }


}
